package com.unytech.wisdomcommunity.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.unytech.wisdomcommunity.R;

/**
 * "二手市场"的页面
 */
public class SecondaryMarketActivity extends AppCompatActivity{

    private ImageView mPhotoGraphIv;
    private TextView tv_1,tv_2,tv_cel;

    public static final int TAKE_PHOTO = 1;
    public static final int CROP_PHOTO = 2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary_market);

        initView();
    }

    private void initView() {
        mPhotoGraphIv = (ImageView) findViewById(R.id.secondary_market_photograph_iv);
        mPhotoGraphIv.setOnClickListener(PhotoGraphIvListener);
    }
    private View.OnClickListener PhotoGraphIvListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopupWindow(v);
        }
    };
    //点击"二手市场"中  上传宝贝 之后 弹出的popupwindow
    private void showPopupWindow(View v) {
        View pupView = LayoutInflater.from(this).inflate(R.layout.popup_window_layout, null);

        tv_1 = ((TextView) pupView.findViewById(R.id.pop_tv_1));
        tv_2 = ((TextView) pupView.findViewById(R.id.pop_tv_2));
        tv_cel = ((TextView) pupView.findViewById(R.id.pop_tv_cel));

        tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"点击打开相册",Toast.LENGTH_SHORT).show();
            }
        });
        //点击开启  手机的摄像头
        tv_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"点击了拍照",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
        final PopupWindow popupWindow = new PopupWindow(pupView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        //把事件拦截下来
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
            }
        });
        popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));

        // 设置好参数之后再show
        //popupWindow.showAsDropDown(v);
        popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);

        tv_cel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK){
                    Intent cropIntent = new Intent("com.android.camera.action.CROP");
                    startActivityForResult(cropIntent,CROP_PHOTO);
                }
                break;
            case CROP_PHOTO:

                break;

            default:
                break;
        }
    }
}